using NLog;
using NLog.Config;
using NLog.Targets;
using UnityEngine;

[Target("UnityConsole")]
public sealed class UnityConsoleTarget : TargetWithLayout
{
    public UnityConsoleTarget()
    {
        Host = "localhost";
    }

    [RequiredParameter]
    public string Host { get; set; }

    protected override void Write(LogEventInfo logEvent)
    {
        string logMessage = Layout.Render(logEvent);

        SendTheMessageToRemoteHost(Host, logMessage, logEvent.Level);
    }

    private void SendTheMessageToRemoteHost(string host, string message, LogLevel level)
    {
        if (level == LogLevel.Info ||
            level == LogLevel.Debug ||
            level == LogLevel.Trace ||
            level == LogLevel.Off)
        {
            Debug.Log(message);
        }
        else if (level == LogLevel.Warn)
        {
            Debug.LogWarning(message);
        }
        else
        {
            Debug.LogError(message);
        }
    }
}
